package com.kurvykurvy.ui.dnd;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class CollageItemTransferHandler implements Transferable {
    private CollageItem item;
    public static DataFlavor COLLAGE_ITEM_FLAVOR;
    static {
        try {
            COLLAGE_ITEM_FLAVOR = new DataFlavor("image/x-collage-item;class=" + CollageItem.class.getCanonicalName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public CollageItemTransferHandler(BufferedImage itemPicture, BufferedImage image) {
        this.item = new CollageItem(itemPicture, image);
    }

    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[] { COLLAGE_ITEM_FLAVOR };
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavor.isMimeTypeEqual(COLLAGE_ITEM_FLAVOR.getMimeType());
    }

    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException,
                                                            IOException {
        if (flavor.equals(COLLAGE_ITEM_FLAVOR)) {
            return item;
        } else {
            return new UnsupportedFlavorException(flavor);
        }
    }
}
